package com.example.wangchencan

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import java.io.File
import java.io.FileInputStream
import java.io.ObjectInputStream

class HistoryActivity : AppCompatActivity() {

    private lateinit var listViewPrimes: ListView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)

        listViewPrimes = findViewById(R.id.listViewPrimes)

        val primes = loadResults()
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, primes)
        listViewPrimes.adapter = adapter
    }

    private fun loadResults(): List<String> {
        val file = File(filesDir, "prime_results.dat")
        if (file.exists()) {
            try {
                val fis = FileInputStream(file)
                val ois = ObjectInputStream(fis)
                val results = ois.readObject() as List<Int>
                ois.close()
                return results.map { it.toString() }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return emptyList()
    }
}
